<template>
  <div class="page-contain">
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="size"
      :pager-count="pager"
      :layout="layout"
      :hide-on-single-page="isSingleHide"
      background
      :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: 'dPagination',
  props: {
    page: { // 当前页码
      type: Number,
      default: 1
    },
    size: { // 每页条数
      type:Number,
      default: 10
    },
    total: { // 总条数
      type: Number,
      default: 0
    },
    pager: { // 页码按钮的数量，当总页数超过该值时会折叠
      type: Number,
      default: 5
    },
    isSingleHide: { // 只有一页时是否隐藏
      type: Boolean,
      default: false
    },
    sizeChange: { // 是否可用更改每页 size 的数量
      type: Boolean,
      default: true
    }
  },
  computed: {
    layout () {
      if (this.$props.sizeChange) {
        return 'total, sizes, prev, pager, next, jumper'
      } else {
        return 'total, prev, pager, next, jumper'
      }
    }
  },
  methods: {
    handleSizeChange(size) { // 切换分页条数（每页 val 条）
      let maxPage = Math.floor(this.$props.total / size)
      if (this.$props.total % size !== 0) maxPage++
      if (this.$props.page > maxPage) {
        this.$emit('update:page', maxPage)
      }
      this.$emit('update:size', size)
      this.$emit('getList')
    },
    handleCurrentChange(page) { // 切换分页（当前页 val）
      this.$emit('update:page', page)
      this.$emit('getList')
    }
  }
}
</script>

<style lang="scss">
.page-contain {
  padding-top: 15px;
  flex-shrink: 0;
  display: flex;
  flex-direction: row-reverse;
}
</style>